# coding = utf-8
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ReadOnlyModelViewSet

from question.models import Investigation, Question, Grade, Options, InvestResult


class InvestigationSerializer(ModelSerializer):
    """
    调查问卷
    """
    class Meta:
        model = Investigation
        fields = ['id', 'name']


class OptionsSerializer(ModelSerializer):
    """
    调查选项
    """
    class Meta:
        model = Options
        fields = ['id', 'question', 'op_name', 'score']


class QuestionSerializer(ModelSerializer):
    """
    调查题干
    """
    invest_name = serializers.CharField(source="invest.name", read_only=True)
    options = OptionsSerializer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = ['id', 'invest', 'invest_name', 'name', 'options']


class GradeSerializer(ModelSerializer):
    """
    调查登记
    """
    d_value = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Grade
        fields = ['id', 'name', 'low_limit_score', 'upper_limit_score', 'd_value']

    def get_d_value(self, obj):
        return obj.upper_limit_score - obj.low_limit_score


class InvestResultSerializer(ModelSerializer):
    """
    调查结果
    """
    invest_name = serializers.CharField(source="invest.name", read_only=True)

    class Meta:
        model = InvestResult
        fields = ['id', 'invest', 'invest_name', 'openid', 'name', 'score', 'grade_name']
